import { observable, action, computed } from 'mobx'
import { GroupList, GroupItem, GroupMemberList, GroupAllMember, GroupMemItem, GroupMemRole, VirualGroupItem } from '../interface/IGroup';
import imsdk from '../net/IMSDK';
// import message from 'antd/es/message';
import chatStore from './ChatStore';
import { FriendItem } from '../interface/IFriend';
import systemStore from './SystemStore';
import webIM from '../net/WebIM';
import { createGroupStore } from '../store/CreatGroupStore';

/**
 * 用户标签组，基于标签对用户进行分组
 */
export class UserGroupStore {

    _groupList: VirualGroupItem[] = [];
    groupList = observable(this._groupList);
    // 所有群成员列表数据
    _groupMemberList: GroupMemberList = new Map();
    @observable groupMemberList = observable(this._groupMemberList);

    @observable selectGroupId: string = '';


    @action setGroupList = (groups: VirualGroupItem[]) => {
        this.groupList.replace(groups)
    }

    @action createUserGroup = async (groupName: string) =>{
        let res = await imsdk.createUserGroup(groupName);
        if( res && res.resultCode == 1){
            webIM.loadUserGroupList()
        }
    }

    @action addUserGroup = async (userId:string, groupName: string) =>{
        let res = await imsdk.createUserGroup(groupName);
        if( res && res.resultCode == 1){
            res= await imsdk.setUserGroupUser(groupName, userId);
        }
        webIM.loadUserGroupList()
        webIM.reloadFriendList()
    }

    /**
     * 将用户加至组
     * @param userIds 
     * @param groupName 
     */
    @action addUsersGroup = async( userIds:string[], groupName: string) =>{
        // for(let id of userIds){
        //     let res = await imsdk.setUserGroupUser(groupName, id);
        // }
        let res = await imsdk.setGroupUsers(groupName, userIds);
        webIM.loadUserGroupList()
        await webIM.reloadFriendList()
    }

    @action updateGroupUsers = async( groupName: string, userIds: string[], oldUsers:string[]) =>{
        let name = groupName
        let delUsers = oldUsers.filter(item=>!userIds.includes(item))
        for(let id of delUsers){
            let res = await imsdk.removeUserGroupUser(name, id);
        }
        let addUsers = userIds.filter(item=>!oldUsers.includes(item))
        for(let id of addUsers){
            let res = await imsdk.setUserGroupUser(name, id);
        }
        webIM.loadUserGroupList()
        await webIM.reloadFriendList()
    }
    
    @action updateGroupInfo = async(groupId:string, groupName:string) =>{
        let res = await imsdk.updateUserGroupInfo(groupId, groupName);
        webIM.loadUserGroupList()
    }

    @action updateUserGroup = async (userId: string, groupNames: string[], sync:boolean = false) =>{
        chatStore.currentChatData.tagGroups = groupNames.map(item=>{
            return new VirualGroupItem({
                name:item,
            })
        })
        if( sync ){
            // db
            const extendInfo = chatStore.currentChatData.extendInfo
            const originTags = extendInfo? (extendInfo.tagNames||[]) : [];
            // 先删除
            let delGroups = originTags.filter(item=> !groupNames.includes(item))
            console.log('delete group', delGroups)
            for (let i = 0; i < delGroups.length; i++) {
                let res = await imsdk.removeUserGroupUser(delGroups[i], userId);
            }
            // 再添加
            let addGroups = groupNames.filter(item=>!originTags.includes(item))  
            console.log('add group', addGroups)
            for (let i = 0; i < addGroups.length; i++) {
                let res = await imsdk.setUserGroupUser(addGroups[i], userId);
            }
            //let res = await imsdk.setGroupUsers()
            webIM.loadUserGroupList();
            await webIM.reloadFriendList()
        }
        
        
    }

    @action removeUserGroupUser = async (userId: string, groupName: string) =>{
        let res = await imsdk.removeUserGroupUser(groupName, userId);
        webIM.loadUserGroupList();
        await webIM.reloadFriendList()
    }
    
    @action removeUserGroup = async ( groupName: string) =>{
        let gs = this.groupList.slice().filter(item => { return item.name != groupName });
        this.setGroupList(gs);
        let res = await imsdk.removeUserGroup(groupName);
        webIM.loadUserGroupList();
        await webIM.reloadFriendList();
    }
    @action updateUserOrGroupOnline( userId:string, online?:boolean ){
        const target = this.groupList.find(item => item.id == userId);
        if( target ){
            target.onlinestate = online? 1 : 0;
        }
    }

}

export default new UserGroupStore();
